package com.kelvin.paopao.module.market.market.order;

import com.kelvin.paopao.api.RetrofitService;
import com.kelvin.paopao.api.responseBean.CommonListResponse;
import com.kelvin.paopao.api.responseBean.OrderModel;
import com.kelvin.paopao.module.base.IBasePresenter;
import com.kelvin.paopao.widgets.EmptyLayout;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by MCK on 2017/5/26.
 */

public class OrderPresenter implements IBasePresenter {
    private OrderActivity mView;

    public OrderPresenter(OrderActivity view) {
        this.mView = view;
    }

    @Override
    public void getData(final boolean isRefresh) {
        RetrofitService.getOrder().doOnSubscribe(new Consumer<Disposable>() {
            @Override
            public void accept(@NonNull Disposable disposable) throws Exception {
                mView.showLoading();
            }
        }).subscribeOn(AndroidSchedulers.mainThread()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<CommonListResponse<OrderModel>>() {
            @Override
            public void accept(@NonNull CommonListResponse<OrderModel> commonListResponse) throws Exception {
                mView.hideLoading();
                if(commonListResponse.getResult()!=null&&commonListResponse.getResult().size()!=0){
                    mView.loadData(commonListResponse.getResult());
                }else{
                    mView.showNoData();
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(@NonNull Throwable throwable) throws Exception {
                mView.showNetError(new EmptyLayout.OnRetryListener() {
                    @Override
                    public void onRetry() {
                        getData(isRefresh);
                    }
                });
            }
        });
    }

    @Override
    public void getMoreData() {

    }
}
